<?php

namespace backend\modules\logs\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\logs\models\AliFaceValidate;

/**
 * AliFaceValidateSearch represents the model behind the search form about `backend\modules\logs\models\AliFaceValidate`.
 */
class AliFaceValidateSearch extends AliFaceValidate
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'audit', 'uid', 'is_manual'], 'integer'],
            [['idcard', 'name', 'created_at', 'client_type', 'ticket_id'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = AliFaceValidate::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'audit' => $this->audit,
            'uid' => $this->uid,
            'is_manual' => $this->is_manual,
        ]);

        $query->andFilterWhere(['like', 'idcard', $this->idcard])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'client_type', $this->client_type])
            ->andFilterWhere(['like', 'ticket_id', $this->ticket_id]);

        if ( ! is_null($this->created_at) && strpos($this->created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}
